function http_post(url, params, successFun, failFun, complete) {
  let baseUrl="https://fyoa.fengyikeji.top";
  // let baseUrl="http://gogu.test";
  let userInfo = wx.getStorageSync('userInfo');
  let header = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'X-Requested-With': 'xmlhttprequest'
  };
  if (userInfo.authorization){
    header.Authorization = userInfo.authorization;
  }

  wx.request({
    url: baseUrl+url,
    method: 'POST',
    data: params,
    header: header,
    success: function (res) {
      if (res.data.code == 401) {
        wx.clearStorageSync();
        alert(res.data.msg);
        wx.removeStorageSync('userInfo');
        wx.removeStorageSync('wechat_user_info');
        
        setTimeout(function () {
        //   wx.navigateBack();
        wx.redirectTo({
          url: '/pages/login/index',
        })
        // go_to("/pages/login/index");
        }, 2000);
        
        // reutrn ;
      } else {
        if(res.data.code==404){
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false,
            confirmText: '确定',
            confirmColor: '#576B95'
          });
          return;
        }else if(res.data.code != 200){
          alert(res.data.msg);
          return ; 
        }
        typeof successFun == 'function' && successFun(res.data)
      }
    },
    fail: function (res) {
      console.log(res)
      alert("网络连接超时，请稍后再试");
      typeof failFun == 'function' && failFun(res.data)
    },
    complete: function (res) {
      typeof completeFun == 'function' && completeFun(res.data)
    }
  });
};
function showLoading() {
  wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
    title: '加载中',
    mask:true,
    icon: 'loading',
  });
};
function hideLoading() {
  wx.hideLoading();
}

function alert(str, time, icon, url) {
  if (!time) {
    time = 2000;
  }
  if (!icon) {
    icon = "none";
  }
  wx.showToast({
    title: str,
    icon: icon,
    mask:true,
    duration: time,//持续的时间
    success: function(){
      if (url){
        setTimeout(function () {
          go_to(url);
        }, time);
      }
    }
  });
}

function go_to(url){
  wx.navigateTo({
    url: url
  });
}

function go_back(object){
  if (!object){
    object = {};
  }
  wx.navigateBack(object)
}
function storage(key,value){
  var result = true;
  if(value || value === null){
    if(value === null){
      wx.removeStorageSync(key)
    }else{
      wx.setStorageSync(key, value);
    }
  }else{
    result = wx.getStorageSync(key);
  }
  return result;
}
function getQueryString (url, name) {
  var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
  var r = url.substr(1).match(reg)
  if (r != null) {
      // console.log("r = " + r)
      // console.log("r[2] = " + r[2])
      return r[2]
  }
  return '';
}

module.exports={
  http_post,
  go_to,
  alert,
  go_back,
  getQueryString,
  showLoading,
  hideLoading,
}